<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Mail list file
 *
 * @package   blocks-email_list
 * @author    Brent Boghosian <brent.boghosian@remote-learner.net>
 * @copyright 2011 Remote Learner - http://www.remote-learner.net
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

/**
 * Class for email lists
 *
 * @author  Brent Boghosian <brent.boghosian@remote-learner.net>
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

class maillist {

    /**
     * list Id
     * @access public
     * @var int $id the list id
     */
    public $id;

    /**
     * list name
     * @access public
     * @var string $listname the lists name
     */
    public $listname;

    /**
     * list owner
     * @access public
     * @var int $owner userid of the lists owner
     */
    public $owner;


    /**
     * Constructor
     *
     * @return object This object
     */
    function maillist() {
        return $this;
    }

    /**
     * This function loads the list from the DB
     * using this objects settings for listname and userid
     *
     * @return object the list object or null on failure.
     *
     */
    function load_list() {
        return $this->get_list($this->listname, $this->owner);
    }

    /**
     * This function gets the list from the DB
     *
     * @param int $listid The lists' unique DB id.
     * @return object The list object or null on failure.
     * @uses $DB
     */
    function get_list_by_id($listid) {
        global $DB;

        if (!empty($listid) && ($lobj = $DB->get_record('block_email_list_lists', array('id' => $listid)))) {
            $this->id       = $listid;
            $this->listname = $lobj->listname;
            $this->owner    = $lobj->owner;
            return $this;
        }
        return null;
    }

    /**
     * This function gets the list from the DB
     *
     * @param string $listname The new list name.
     * @param int    $userid   The lists' owner
     * @return object the list object or null on failure.
     * @uses $DB Gets records
     */
    function get_list($listname, $userid) {
        global $DB;

        // validate input
        if (empty($listname) || !$userid) {
            return null;
        }

        $listname = trim($listname);

        if (empty($listname)) {
            return null;
        }

        $criteria =  array('listname' => $listname, 'owner' => $userid);

        if ($listobj = $DB->get_record('block_email_list_lists', $criteria)) {
            $this->id       = $listobj->id;
            $this->listname = $listobj->listname;
            $this->owner    = $listobj->owner;
            return $this;
        }
        return null;
    }

    /**
     * This function creates a new list
     *
     * @param string $listname The new list name.
     * @param int    $userid   The lists' owner
     * @return int -1 if list already exists with listname,
     *              0 on error, 1 on success.
     * @uses $DB Inserts records
     */
    function new_list($listname, $userid) {
        global $DB;

        // validate input
        if (empty($listname) || !$userid) {
            return 0;
        }

        $listname = trim($listname);

        if (empty($listname)) {
            return 0;
        }

        $this->listname = $listname;
        $this->owner    = $userid;
        $this->id       = 0;

        if ($this->get_list($listname, $userid)) {
            return -1; // list already exists
        }

        if ($DB->insert_record('block_email_list_lists', $this) ) {
            // get list id field
            $this->load_list();
            return 1;
        }
        return 0;
    }

    /**
     * This function edits name of the list
     *
     * @param string $listname The new list name.
     * @return int -1 if list already exists with listname,
     *              0 on error, 1 on success.
     * @uses $DB Updates records
     */
    function edit_list($listname) {
        global $DB;

        // validate input
        if (empty($listname) || empty($this->id)) {
            return 0;
        }

        $listname = trim($listname);

        if (empty($listname)) {
            return 0;
        }

        if ($this->get_list($listname, $this->owner)) {
            return -1; // list already exists
        }

        $this->listname = $listname;
        return $DB->update_record('block_email_list_lists', $this) ? 1 : 0;
    }

    /**
     * This function deletes this list, removing all members
     *
     * @return boolean true on success, false on error
     * @uses $DB Deletes records
     */
    function del_list() {
        global $DB;

        if (empty($this->id)) {

            if (!empty($this->listname) && !empty($this->owner)) {
                $this->load_list();
            }
        }

        if (!empty($this->id)) {
            $DB->delete_records('block_email_list_members', array('listid' => $this->id));

            if ($DB->delete_records('block_email_list_lists', array('id' => $this->id))) {
                $this->id = 0;
                $this->owner = 0;
                $this->listname = null;
                return true;
            }
        }

        return false;
    }

    /**
     * This function adds new member to the list
     *
     * @param int $userid The new member to add to list
     * @return int 1 on success, 0 on input or db error,
     *            -1 if user already in the list.
     * @uses $DB Gets records and inserts records
     */
    function add_member($userid) {
        global $DB;

        // validate input
        if (!$userid || empty($this->id)) {
            return 0;
        }

        $criteria = array('listid' => $this->id, 'userid' => $userid);
        // check if user already in list
        if ($DB->get_record('block_email_list_members', $criteria)) {
            return -1;
        }

        $member = new stdClass;
        $member->listid = $this->id;
        $member->userid = $userid;
        return $DB->insert_record('block_email_list_members', $member) ? 1: 0;
    }

    /**
     * This function removes a member from the list
     *
     * @param int $userid The new member to remove from the list
     * @return bool True on success, false on error.
     * @uses $DB Deletes records
     */
    function del_member($userid) {
        global $DB;

        // validate input
        if (!$userid) {
            return false;
        }

        $criteria = array('listid' => $this->id, 'userid' => $userid);
        return $DB->delete_records('block_email_list_members', $criteria);
    }


    /**
     * This function retrieves list members from DB
     *
     * @return array of userids members of the list
     * @uses $DB Gets records
     */
    function get_members() {
        global $DB;

        if (empty($this->id)) {
            return null;
        }

        return $DB->get_records('block_email_list_members', array('listid' => $this->id));
    }

}